﻿using System.Collections.Generic;
using System.Xml;
using System.Xml.Serialization;
using T4UtilityBelt.DataDesigns;
using T4UtilityBelt.Serializers;
using T4UtilityBelt.UxDesigns.Bindings;
using T4UtilityBelt.UxDesigns.Controls;
using T4UtilityBelt.UxDesigns.Screens.Renders;

namespace T4UtilityBelt.UxDesigns.Screens
{
    public class EntityScreen<TView, TRenderer> : Screen<TView, TRenderer>
        where TView : View<TView, TRenderer>
        where TRenderer : class, IViewRenderer<TView, TRenderer>
    {
        public EntityScreen()
        {
            //StandardConstructor();
        }

        public EntityScreen(string entityName) 
        {
            EntityName = entityName;
            Name = entityName;
            //StandardConstructor();
        }

        public EntityScreen(string entityName, string screenName)
        {
            EntityName = entityName;
            Name = screenName;
        }

        //public override string Name
        //{
        //    get
        //    {
        //        if (string.IsNullOrEmpty(base.Name))
        //        {
        //            return EntityName;
        //        }
        //        return base.Name;
        //    }
        //    set
        //    {
        //        base.Name = value;
        //    }
        //}

        public string EntityName { get; set; }

        [XIgnore]
        public Entity Entity { get; set; }

        [XIgnore]
        public override ControlCollection Controls
        {
            get
            {
                return base.Controls;
            }
        }

        public override IEnumerable<Control> AllControls()
        {
            if (Controls.Count > 0)
            {
                foreach (Control c in Controls)
                {
                    yield return c;
                    foreach (Control cc in c.AllControls())
                    {
                        yield return cc;
                    }
                }
            }
            else
            {
                foreach (var p in Entity.Properties)
                {
                    yield return Control.Create(p);
                }
            }
        }

        //private void StandardConstructor()
        //{
        //    //RenderHelpText = true;
        //    //Controls.DefaultPropertyDataContext = "Entry";
        //    //Controls.DefaultIsEnabledProperty = "CanCreate";

        //    _mainGrid = new GridControl();

        //    GridRow row = new GridRow();
        //    _mainGrid.Rows.Add(row);
        //    _titlePanel = new StackPanelControl();
        //    row.Controls.Add(_titlePanel);

        //    LabelControl title = new LabelControl { Text = this.Name };

        //    _titlePanel.Controls.Add(title);

        //    row = new GridRow();
        //    _mainGrid.Rows.Add(row);
        //    _content = new StackPanelControl();
        //    row.Controls.Add(_content);
        //    _content.DataContext = new Binding("Data");

        //    row = new GridRow();
        //    _mainGrid.Rows.Add(row);
        //    _buttonsPanel = new StackPanelControl();
        //    row.Controls.Add(_buttonsPanel);

        //    Controls.Add(_mainGrid);
        //}

        internal override void FixModel(T4UtilityBelt.DataDesigns.EntityModel model)
        {
            base.FixModel(model);

            Entity = model.GetEntity(EntityName);
            if (Entity != null)
            {
                foreach (Control ctl in Controls)
                {
                    ctl.FixModel(Entity);
                }
            }
        }

        //private GridControl _mainGrid;
        //public GridControl MainGrid
        //{
        //    get { return _mainGrid; }
        //}

        //private StackPanelControl _titlePanel;
        //public StackPanelControl TitlePanel
        //{
        //    get { return _titlePanel; }
        //}

        //public ControlCollection TitleControls
        //{
        //    get { return _titlePanel.Controls; }
        //}

        //private StackPanelControl _content;
        //public StackPanelControl Content
        //{
        //    get { return _content; }
        //}

        //public ControlCollection ContentControls
        //{
        //    get { return _content.Controls; }
        //}

        //private StackPanelControl _buttonsPanel;
        //public StackPanelControl ButtonPanel
        //{
        //    get { return _buttonsPanel; }
        //}

        //public ControlCollection ButtonControls
        //{
        //    get { return _buttonsPanel.Controls; }
        //}
    }
}